package a10_动态规划;

import java.util.Arrays;

/**
 * <p>
 * a23_零钱兑换复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/2/18
 */
public class a23_零钱兑换复习1 {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;

        //完全背包：都是顺序遍历
        //是经典问题，先谁后谁都行
        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                if(dp[j-coins[i]] != Integer.MAX_VALUE){
                    dp[j] = Math.min(dp[j],dp[j-coins[i]]+1);
                }
            }
        }

        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
}
